import { observable, action, asMap } from 'mobx';
import * as API from '../apis/center';
import * as Api_list from '../apis/list';
import { WeaForm } from 'comsMobx';
import {WeaLocaleProvider} from 'ecCom';
import { message } from 'antd';
const getLabel = WeaLocaleProvider.getLabel;
export class centerStore {
  @observable layoutInfo = {};
  @observable settingDataMap  = observable.map ? observable.map({}): asMap({}); //存放弹框设置元素信息

  @observable loadingStatusObj = {
    statisticHasLoaded: false,
    rejectHasLoaded: false,
    startHasLoaded: false,
    analysisHasLoaded: false,
    superviseHasLoaded: false,
    superviseHasLoad: false,
    todolistHasLoad: false,
    attentionHasLoad: false,
    responseHasLoad: false
  }

  @observable statisticInfo = {};
  @observable statisticTabContentInfo = []; //tab内容数据

  @observable superviseData = {};
  @observable responseData = [];
  
  @observable rejectedStatisData = {}
  @observable startWfData = {};
  @observable reportListData = {};

  @observable attentionData = {};
  @observable todoData = {};

  @observable settingForm = new WeaForm();
  @observable settingFormCondition = [];

  @observable orderColVisible = false;
  @observable eFieldNewVisible = false;
  @observable showTypeNewVisible = false;
  @action('修改每个模块的加载状态')
  changeLoadStatus = (obj={}) => {
    this.loadingStatusObj = {...this.loadingStatusObj, ...obj};
  }
  @action('获取布局信息')
  getLayoutInfo = () => {
    API.initLayoutInfo().then(data => {
      this.layoutInfo = data
    })
  }

  @action centerSetState = (obj) => {
    for(let key in obj) {
      this[key]= obj[key]
    }
  }
  @action('获取流程中心统计数据')
  getWfCenterStatisticInfo = (params={}, callback) => {
    API.getWfStatisticCenterInfo(params).then(data => {
      this.changeLoadStatus({statisticHasLoaded: true})
      this.statisticTabContentInfo = data.statisticInfo;
      this.statisticInfo = data;
      callback && callback({tabs: data.tabs, statisticScope: data.statisticsScope});
    })
  }
  
  @action('元素Tab切换(需要走接口)')
  getTabSwitch = (origin='', params) => {
    switch(origin) {
      case('staticCenter'): {
        API.getWfStatisticCenterTabInfo(params).then(data => {
            this.changeLoadStatus({statisticHasLoaded: true});
            this.statisticTabContentInfo=data.statisticInfo;
        })
      }; break; 
      case('attention'): {
        let that = this;
        API.getAttentionTabList(params).then( data => {
          this.attentionData = {};
          this.attenTimer = setTimeout(() => {
            clearTimeout(that.attenTimer);
            that.attentionData = data;
            that.showUnoperators(that.attentionData.datas, 'atten');
            this.getWrap(that.attentionData);
            that.changeLoadStatus({attentionHasLoad: true});
          }, 100);
        })
      }; break;
     case('backlog'): {
       let that = this;
        API.getToDoTabList(params).then( data =>{
          this.todoData = {};
          this.todoTimer = setTimeout(()=> {
            clearTimeout(that.todoTimer);
            this.todoData = data;
            this.showUnoperators(this.todoData.datas, 'todo');
            that.changeLoadStatus({todolistHasLoad: true});
         },100);
        })
     };break;
    }
  }
  
  //遍历待办关注数据为a标签包div
  getWrap = (data = []) => {
    data.datas.length > 0 && data.datas.map ( v => {
      if(v.requestname) {
        v.requestname = "<div class='wf-title-wrap' style='width:98%'  title='" + v.requestnametitle + "'>" + v.requestname +"</div>"
      }
    })
  }


  @action('获取tab顶部数量')
  getTabCount = (data,callback) => {
    let params = {source:'wfcenter_todo'};
    API.getTabCount(params).then( tab => {
      let result = this.changeTab(data, tab.totalcount)
      callback && callback(result);
    })
  }


  showUnoperators(datas = [], fromWhere=''){
    let newDatas = [];
    if(datas.length > 0){
        datas.map(data => {
            newDatas.push({"requestid":data.requestid,"userid":data.userid, moduleId: fromWhere});
        })
    }
    Api_list.getUnoperators({datas:JSON.stringify(newDatas),actiontype:'batch',showAll:'false'}).then(unoperators =>{
        for(var unopt in unoperators) {
            jQuery("#"+unopt).html(unoperators[unopt]);
          }
    });
}
  @action('获取待办中心数据')
  getCenterWf = (params = {}, callback) =>  {
    API.getToDoList().then(data => {
      this.todoData = data;
      this.showUnoperators(data.datas, 'todo');
      API.getTabCount({source:'wfcenter_todo'}).then( tab => {
        this.changeTab(data.tabs, tab.totalcount);
        this.changeLoadStatus({todolistHasLoad: true});
        callback && callback(data);
      })
    })
  }
  //tab项拼接count
  changeTab = (tabarr=[], tabobj={}) => {
    let result = [];
    tabarr.map( (v,index) => { v['index'] = index} );
    for(let key in tabobj) {
      tabarr.map( v => {
       if(v.tabname  == key) {
         if(tabobj[key] > 99){
           v.count = '99+';
         } else {
           v.count = tabobj[key];
         }
       }
     })
    }
    result = tabarr;
    return result;
  }
  @action('获取关注事宜的数据')
  getAttentionList = (params = {}, callback) => {
    API.getAttentionList().then(data => {
      this.attentionData = data;
      this.showUnoperators(data.datas, 'atten');
      this.getWrap(this.attentionData);
      this.changeLoadStatus({attentionHasLoad: true})
      callback && callback(data);
    })
  }

  @action('关注tag删除')
  delAttentionTag = (params,callback) => {
    API.saveAttention(params).then((data)=>{
      if(data.success==="1"){
        typeof callback == 'function' && callback();
      } 
    }); 
  }


  @action('获取督办数据') 
  getSuperviseStatisticInfo = (params ={}) => {
    API.getSuperviseStatisticInfo(params).then(data => {
      this.changeLoadStatus({superviseHasLoad: true})
      this.superviseData = data;
    })
  }
  @action('获取退回统计数据')
  getRejectStatisticInfo = (params ={}, callback) => {
    API.getRejectStatisticInfo(params).then(data => {
      this.changeLoadStatus({rejectHasLoaded: true})
      this.rejectedStatisData = data;
      callback && callback(data);
    })
  }
  

  @action('获取新反馈元素数据')
  getResList = (params={}) => {
    API.getResList(params).then( data => {
      this.responseData = [];
      this.responseData.push(data);
      this.changeLoadStatus({responseHasLoad: true});
    })
  }


  @action('获取设置弹框信息')
  getSettingDialogInfo = (params={}, isSetting=false) =>{
    let type = params.eleType;
    this.settingFormCondition = [];
    this.settingForm = new WeaForm();
    let formInitData = [{items: []}]; //传入WeaForm初始化的数据结构一定要是这样的！
    API.getSettingDialogInfo(params).then(data => {
      this.settingDataMap.set(type, data);
      // this.settingFormCondition.push(data.condition);
      formInitData[0].items = data.condition.items;
      this.settingFormCondition = formInitData;
      this.settingForm.initFormFields(this.settingFormCondition);
    })
  }

  @action('弹框设置保存')
  getSetSave = (params) => {
    API.doSetSave(params).then( data => {
      if (data.result == "success") {
        message.success(getLabel(83551, "保存成功"));
      }else  {
        message.error(getLabel(84544, "保存失败"));
      }
    })
  }
  @action('获取报表分析元素数据')
  getReportList = (params={}, callback) => {
    API.getReportList(params).then( data => {
      this.changeLoadStatus({analysisHasLoaded: true})
      this.reportListData = data;
      callback && callback(data);
    })
  }


  @action('获取发起流程元素')
  getWfInfo = (params={}, callback) => {
    API.getWfInfo(params).then( data => {
      this.changeLoadStatus({startHasLoaded: true})
      this.startWfData = data;
      this.startWfData =this.formatData(data);
      callback && callback(data);
    })
  }

  //发起流程数据调整
  formatData = (info) => {
    let obj = {
      datas: [],
      tabs: info.tabs,
      unSupportSPAWf: info.unSupportSPAWf
    }
    let oftenUseArr = [];
    let oftenUseTemp = [];
    let collectArr = [];
    let collectTemp = [];
    info.datas && info.datas.map ( item => {
      item.wfbeans.length > 0 && item.wfbeans.map ( v => {
        if(v.usedtodo == '1') {
          oftenUseArr.push(v)
        }
        if(v.wfColl != '0') {
          collectArr.push(v);
        }
      })
    })
    oftenUseTemp =  oftenUseArr.sort((a, b) => {
      return a.usedtodoorder - b.usedtodoorder
    }).slice(0, 10);
    collectTemp = collectArr.slice(0,10);
    info.tabs && info.tabs.map ( v => {
      let key = v.key;
      if(key == '3') { //常用流程
        obj.datas.push({key: v.key, datas: oftenUseTemp})
      } else if( key == '2') { //我的收藏
        obj.datas.push({key: v.key, datas: collectTemp})
      }
    }) 
    return obj;
  }
 
  @action('清理store')
  clearStore = () => {
    this.settingDataMap  = observable.map ? observable.map({}): asMap({}); //存放弹框设置元素信息
    this.loadingStatusObj = {
      statisticHasLoaded: false,
      rejectHasLoaded: false,
      startHasLoaded: false,
      analysisHasLoaded: false,
      superviseHasLoaded: false,
      superviseHasLoad: false,
      todolistHasLoad: false,
      attentionHasLoad: false,
      responseHasLoad: false
    };
    this.statisticInfo = {};
    this.statisticTabContentInfo = []; //tab内容数据
    this.superviseData = {};
    this.responseData = [];
    this.rejectedStatisData = {}
    this.startWfData = {};
    this.reportListData = {};
    this.attentionData = {};
    this.todoData = {};
    this.settingForm = new WeaForm();
    this.settingFormCondition = [];
    this.orderColVisible = false;
    this.eFieldNewVisible = false;
    this.showTypeNewVisible = false;
  }
}



